package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SupplierBean;
import com.project.dao.ISupplierDao;
import com.project.service.ISupplierService;
import com.project.util.InfoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lyl
 * description:
 */
@Service
@Transactional
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private ISupplierDao iSupplierDao;

    @Override
    public Page<SupplierBean> showSupplierByPage(int pageNum, String supplierName, String supplierScope) {
        QueryWrapper<SupplierBean> queryWrapper = new QueryWrapper<>();
        if (!"1".equals(supplierName)) {
            queryWrapper.like("supplierName", supplierName);
        }
        if (!"1".equals(supplierScope)) {
            queryWrapper.like("supplierScope", supplierScope);
        }
        return iSupplierDao.selectPage(new Page<>(pageNum, 3), queryWrapper);
    }

    @Override
    public SupplierBean showSupplierById(int supplierId) {
        return iSupplierDao.selectById(supplierId);
    }

    @Override
    public InfoResult addSupplier(SupplierBean supplierBean) {
        int x = iSupplierDao.insert(supplierBean);
        if (x == 1) {
            return new InfoResult(0, "添加成功");
        }
        return new InfoResult(1, "添加失败");
    }

    @Override
    public InfoResult updateSupplier(int supplierId, String supplierPeo, String tel) {
        SupplierBean su = iSupplierDao.selectById(supplierId);
        su.setSupplierTel(tel);
        su.setSupplierPeo(supplierPeo);
        int x = iSupplierDao.updateById(su);
        if (x == 1) {
            return new InfoResult(0, "修改成功");
        }
        return new InfoResult(1, "修改失败");
    }

    @Override
    public List<SupplierBean> showAllSupplier() {
        return iSupplierDao.selectList(null);
    }
}
